package ims.db.delete.product;

import ims.db.AbstractDAO;
import ims.db.AbstractDbRequest;
import ims.db.AbstractDbResponse;

import java.sql.SQLException;
import java.sql.Statement;

public class DeleteProductDAO extends AbstractDAO {

	public DeleteProductDAO() throws ClassNotFoundException, SQLException {
		super();
	}

	@Override
	public AbstractDbResponse execute(AbstractDbRequest dbRequest) 
	{
		DeleteProductDbResponse response = new DeleteProductDbResponse();
		DeleteProductDbRequest request = (DeleteProductDbRequest)dbRequest;
        try 
        {
        	Statement stmt = conn.createStatement();
        	StringBuffer sql = new StringBuffer();
        	String prodId = request.getPid();
        	sql.append("delete from Product where pid ='").append(prodId).append("'");
        	
    		int retCd = stmt.executeUpdate(sql.toString());
    		if (retCd == 1)
    			response.setMsg("Product: " + request.getProdName()+" deleted.");
    		else 
    			response.setMsg("Unable to delete product type: " + request.getProdName());
            stmt.close();
            conn.close();
        } 
        catch (SQLException sqlEx)
        {
        	sqlEx.printStackTrace();
        }
        return response;
	}
}
